/*
 * Copyright 2007 Xplayat, S.L. All rights reserved.
 * Propiedad de Xplayat. 
 */
package org.apache.myfaces.custom.ysf.message;

import java.io.IOException;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;

import javax.faces.context.ResponseWriter;

import org.apache.myfaces.renderkit.html.util.AddResourceFactory;
import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlMessageRendererBase;
import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRendererUtils;

public class HtmlMessageRenderer extends org.apache.myfaces.renderkit.html.ext.HtmlMessageRenderer {
	
	@Override
    protected void renderSingleFacesMessage(FacesContext facesContext, UIComponent message, FacesMessage facesMessage, String messageClientId)
    	throws IOException
   	{
        
	  // determine style and style class
	  String[] styleAndClass = HtmlMessageRendererBase.getStyleAndStyleClass(message, facesMessage.getSeverity());
	  String style = styleAndClass[0];
	  String styleClass = styleAndClass[1];

	  String summary = getSummary(facesContext, message, facesMessage, messageClientId);
	  String detail = getDetail(facesContext, message, facesMessage, messageClientId);
	  String title = getTitle(message);
          
	  boolean tooltip = isTooltip(message);

	  if (title == null && tooltip)
	  {
	      title = summary;
	  }

	  ResponseWriter writer = facesContext.getResponseWriter();

	  boolean span = false;


	  if (message.getId() != null && !message.getId().startsWith(UIViewRoot.UNIQUE_ID_PREFIX))
	  {
	      span = true;

	      writer.startElement(HTML.SPAN_ELEM, message);

	      HtmlRendererUtils.writeIdIfNecessary(writer, message, facesContext);

	      HtmlRendererUtils.renderHTMLAttributes(writer, message, HTML.MESSAGE_PASSTHROUGH_ATTRIBUTES_WITHOUT_TITLE_STYLE_AND_STYLE_CLASS);
	  }
	  else
	  {
	      span = HtmlRendererUtils.renderHTMLAttributesWithOptionalStartElement(
	              writer, message, HTML.SPAN_ELEM, HTML.MESSAGE_PASSTHROUGH_ATTRIBUTES_WITHOUT_TITLE_STYLE_AND_STYLE_CLASS);
	  }

	  span |= HtmlRendererUtils.renderHTMLAttributeWithOptionalStartElement(writer, message, HTML.SPAN_ELEM, HTML.TITLE_ATTR, title, span);
	  span |= HtmlRendererUtils.renderHTMLAttributeWithOptionalStartElement(writer, message, HTML.SPAN_ELEM, HTML.STYLE_ATTR, style, span);
	  span |= HtmlRendererUtils.renderHTMLAttributeWithOptionalStartElement(writer, message, HTML.SPAN_ELEM, HTML.STYLE_CLASS_ATTR, styleClass, span);



          writer.startElement( HTML.IMG_ELEM, message );
          String src = AddResourceFactory.getInstance( facesContext ).getResourceUri( facesContext, HtmlMessageRenderer.class, "img/error.gif" );
          writer.writeURIAttribute( HTML.SRC_ATTR, src, null );
              writer.writeAttribute( HTML.TITLE_ATTR, detail, null );
              writer.writeAttribute( HTML.ALT_ATTR, detail, null );
          writer.endElement( HTML.IMG_ELEM );



/*	  boolean showSummary = isShowSummary(message) && (summary != null);
	  boolean showDetail = isShowDetail(message) && (detail != null);

	  if (showSummary && !(title == null && tooltip))
	  {
	      writer.writeText(summary, null);
	      if (showDetail)
	      {
	          writer.writeText(" ", null);
	      }
	  }

	  if (showDetail)
	  {
	      writer.writeText(detail, null);
	  }
*/
	  if (span)
	  {
	      writer.endElement(org.apache.myfaces.shared_tomahawk.renderkit.html.HTML.SPAN_ELEM);
	  }
    	
	}
}
